
$identify("org/mathdox/formulaeditor/modules/relation1/eq.js");

$require("org/mathdox/parsing/ParserGenerator.js");
$require("org/mathdox/formulaeditor/parsing/openmath/OpenMathParser.js");
$require("org/mathdox/formulaeditor/parsing/expression/ExpressionParser.js");
$require("org/mathdox/formulaeditor/semantics/MultaryOperation.js");
$require("org/mathdox/formulaeditor/semantics/Keyword.js");

$main(function(){

  var symbol = {
    onscreen : "=",
    openmath : null, // use default with model:cd and model:name
    mathml   : "<mo>=</mo>"
  };

  /**
   * Define a semantic tree node that represents relation1.eq.
   */
  org.mathdox.formulaeditor.semantics.Relation1Eq =
    $extend(org.mathdox.formulaeditor.semantics.MultaryOperation, {

      symbol : {

        onscreen : symbol.onscreen,
        openmath : "<OMS cd='relation1' name='eq'/>",
        mathml   : symbol.mathml

      },

      precedence : 110

    });
  
  /**
   * Extend the OpenMathParser object with parsing code for relation1.eq.
   */
  org.mathdox.formulaeditor.parsing.openmath.OpenMathParser =
    $extend(org.mathdox.formulaeditor.parsing.openmath.OpenMathParser, {

    /**
     * Returns an equality object based on the OpenMath node.
     */
    handleRelation1Eq : function(node) {

      // parse the children of the OMA
      var children = node.getChildNodes();
      var operands = [];
      for (var i=1; i<children.length; i++) {
        operands.push(this.handle(children.item(i)));
      }

      // construct the corresponding object
      var result = new org.mathdox.formulaeditor.semantics.Relation1Eq();
      result.initialize.apply(result, operands);
      return result;

    }

  });

  org.mathdox.formulaeditor.parsing.openmath.KeywordList["relation1__eq"] = new org.mathdox.formulaeditor.semantics.Keyword("relation1", "eq", symbol, "infix");

  /**
   * Extend the ExpressionParser object with parsing code for the equality sign.
   */
  var semantics = org.mathdox.formulaeditor.semantics;
  var pG = new org.mathdox.parsing.ParserGenerator();

  if ( "=" == "=" ) {
    // only one expression, same on screen
    org.mathdox.formulaeditor.parsing.expression.ExpressionParser =
      $extend(org.mathdox.formulaeditor.parsing.expression.ExpressionParser, {

      // expression110 = eq | super.expression110
      expression110 : function() {
        var parent = arguments.callee.parent;
        pG.alternation(
          pG.rule("relation1eq"),
          parent.expression110).apply(this, arguments);
      },

      // relation1eq = 
      //    expression110 "=" expression120
      relation1eq :
        pG.transform(
          pG.concatenation(
            pG.rule("expression120"),
            pG.literal("="),
            pG.rule("expression120")
          ),
          function(result) {
            return new semantics.Relation1Eq(result[0], result[2]);
          }
        )

    });
  } else { // allow alternative as displayed on the screen
    org.mathdox.formulaeditor.parsing.expression.ExpressionParser =
      $extend(org.mathdox.formulaeditor.parsing.expression.ExpressionParser, {

      // expression110 = relation1eq | 
      //   relation1eqalt | super.expression110
      expression110 : function() {
        var parent = arguments.callee.parent;
        pG.alternation(
          pG.rule("relation1eq"),
          pG.rule("relation1eqalt"),
          parent.expression110).apply(this, arguments);
      },

      // relation1eq = 
      //    expression110 "=" expression120
      relation1eq :
        pG.transform(
          pG.concatenation(
            pG.rule("expression120"),
            pG.literal("="),
            pG.rule("expression120")
          ),
          function(result) {
            return new semantics.Relation1Eq(result[0], result[2]);
          }
        ),

      // relation1eqalt = 
      //    expression110 "=" expression120
      relation1eqalt :
        pG.transform(
          pG.concatenation(
            pG.rule("expression120"),
            pG.literal("="),
            pG.rule("expression120")
          ),
          function(result) {
            return new semantics.Relation1Eq(result[0], result[2]);
          }
        )

    });
  }

});
